package com.cuit.zuke.order.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cuit.zuke.common.vo.QuarterlyDataVo;
import com.cuit.zuke.common.vo.WeeklyDataVo;
import com.cuit.zuke.order.mapper.GoodsAndHotelMapper;
import com.cuit.zuke.order.mapper.GoodsOrderMapper;
import com.cuit.zuke.order.mapper.HotelOrderMapper;
import com.cuit.zuke.order.model.entity.GoodsOrder;
import com.cuit.zuke.order.model.entity.HotelOrder;
import com.cuit.zuke.order.service.GoodsAndHotelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @program: zuke
 * @description: 商品和酒店公共服务接口实现类
 * @author: KamTang
 * @create: 2022-06-06 10:24
 **/
@Service
public class GoodsAndHotelServiceImpl implements GoodsAndHotelService {

    @Autowired
    private GoodsAndHotelMapper goodsAndHotelMapper;

    @Autowired
    private GoodsOrderMapper orderMapper;

    @Autowired
    private HotelOrderMapper hotelOrderMapper;

    @Override
    public Map<String, List<?>> getWeeklyGoodsAndHotelOrderData() {
        // 近一周商品订单数据
        List<WeeklyDataVo> goodsOrderData = goodsAndHotelMapper.getWeeklyGoodsOrderData();
        // 获取周
        List<String> weekend = goodsOrderData.stream().map(WeeklyDataVo::getWeekend).collect(Collectors.toList());
        // 获取近一周商品订单详细单数
        List<Integer> goodsOrder = goodsOrderData.stream().map(WeeklyDataVo::getCount).collect(Collectors.toList());
        // 近一周酒店订单数据
        List<WeeklyDataVo> hotelOrderData = goodsAndHotelMapper.getWeeklyHotelOrderData();
        // 获取近一周酒店订单详细单数
        List<Integer> hotelOrder = hotelOrderData.stream().map(WeeklyDataVo::getCount).collect(Collectors.toList());
        // 获取商品订单总数
        int goodsOrderCount = orderMapper.selectCount(new QueryWrapper<GoodsOrder>());
        // 获取酒店订单总数
        int hotelOrderCount = hotelOrderMapper.selectCount(new QueryWrapper<HotelOrder>());
        // 用List存放订单总数
        List<Integer> orderCount = new ArrayList<>();
        orderCount.add(goodsOrderCount + hotelOrderCount);
        // 存放最后结果
        Map<String, List<?>> map = new HashMap<>(5);
        // 设置标题
        List<String> titles = new ArrayList<>();
        titles.add("酒店订单");
        titles.add("商品订单");
        map.put("xAbscissa", weekend);
        map.put("redData", hotelOrder);
        map.put("blueData", goodsOrder);
        map.put("titles", titles);
        map.put("orderTotal", orderCount);
        return map;
    }

    @Override
    public BigDecimal getGoodsOrderCountMoney() {
        return goodsAndHotelMapper.getGoodsCount();
    }

    @Override
    public List<QuarterlyDataVo> getQuarterlyGoodsMoney() {
        return goodsAndHotelMapper.getQuarterlyGoodsMoney();
    }

    @Override
    public BigDecimal getHotelOrderCountMoney() {
        return goodsAndHotelMapper.getHotelCount();
    }

    @Override
    public List<QuarterlyDataVo> getQuarterlyHotelMoney() {
        return goodsAndHotelMapper.getQuarterlyHotelMoney();
    }

    @Override
    public Map<String, List<?>> getQuarterlyGoodsAndHotelMoney() {
        // 商品总收益
        BigDecimal goodsMoneyCount = this.getGoodsOrderCountMoney();
        // 酒店总收益
        BigDecimal hotelMoneyCount = this.getHotelOrderCountMoney();
        List<BigDecimal> total = new ArrayList<>(Collections.singletonList(goodsMoneyCount.add(hotelMoneyCount)));
        // 各个季度商品收益情况
        List<QuarterlyDataVo> quarterlyGoodsMoney = this.getQuarterlyGoodsMoney();
        // 获取商品每季度具体收益 - 金额
        List<BigDecimal> goodsMoney = getQuarterlyMonet(quarterlyGoodsMoney);
        // 各个季度酒店收益情况
        List<QuarterlyDataVo> quarterlyHotelMoney = this.getQuarterlyHotelMoney();
        // 获取酒店每季度具体收益 - 金额
        List<BigDecimal> hotelMoney = getQuarterlyMonet(quarterlyHotelMoney);
        // 结果封装
        Map<String, List<?>> map = new HashMap<>(6);
        List<String> quarters = new ArrayList<>(Arrays.asList("第一季度", "第二季度", "第三季度", "第四季度"));
        // JSON数组
        JSONArray jsonArray = new JSONArray();
        for (int i = 0; i < quarters.size(); i++) {
            // JSON对象
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("value", goodsMoney.get(i).add(hotelMoney.get(i)));
            jsonObject.put("name", quarters.get(i));
            jsonArray.add(jsonObject);
        }
        List<String> title = new ArrayList<>(Arrays.asList("酒店收益", "商品收益"));
        map.put("xAbscissa", quarters);
        map.put("redData", hotelMoney);
        map.put("blueData", goodsMoney);
        map.put("titles", title);
        map.put("moneyTotal", total);
        map.put("goodsAddHotelMoneyData", jsonArray);
        return map;
    }

    /**
     * 获取每季度具体收益
     *
     * @param quarterlyDataVoList List<QuarterlyDataVo>
     * @return List<BigDecimal>
     */
    private List<BigDecimal> getQuarterlyMonet(List<QuarterlyDataVo> quarterlyDataVoList) {
        List<Integer> quarterList = quarterlyDataVoList.stream().map(QuarterlyDataVo::getQuarter).collect(Collectors.toList());
        List<BigDecimal> moneyList = quarterlyDataVoList.stream().map(QuarterlyDataVo::getMoneyTotal).collect(Collectors.toList());
        List<BigDecimal> resultList;
        // 判断是否为空
        if (quarterList.size() == 0) {
            resultList = new ArrayList<>(
                    Arrays.asList(new BigDecimal(0),
                            new BigDecimal(0),
                            new BigDecimal(0),
                            new BigDecimal(0)));
        } else {
            resultList = new ArrayList<>();
            for (int i = 0; i < quarterList.size(); i++) {
                for (int j = i; j < i + 1; j++) {
                    if (j + 1 == quarterList.get(i)) {
                        resultList.add(j, moneyList.get(i));
                    } else {
                        resultList.add(j, new BigDecimal(0));
                    }
                }
            }
        }

        return resultList;
    }

}
